package com.gxuwz.controller;

import com.gxuwz.model.entity.Department;
import com.gxuwz.model.vo.Result;
import com.gxuwz.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/departments")
@CrossOrigin
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    // 获取所有二级学院
    @GetMapping("/list")
    public Result<List<Department>> listDepartments() {
        List<Department> departments = departmentService.getAllDepartments();
        return Result.succeed(departments);
    }

    // 根据 depID 获取二级学院详情
    @GetMapping("depID/{depID}")
    public Result<Department> getDepartmentById(@PathVariable String depID) {
        Department department = departmentService.getDepartmentByDepID(depID);
        return department != null ? Result.succeed(department) : Result.error("未找到该二级学院");
    }

    // 根据 depName 获取二级学院详情
    @GetMapping("/name/{depName}")
    public Result<Department> getDepartmentByDepName(@PathVariable String depName) {
        Department department = departmentService.getDepartmentByDepName(depName);
        return department != null ? Result.succeed(department) : Result.error("未找到该二级学院");
    }

    // 根据 depName 获取二级学院详情
    @GetMapping("/{ID}")
    public Result<Department> getDepartmentByID(@PathVariable int ID) {
        Department department = departmentService.getDepartmentByID(ID);
        return department != null ? Result.succeed(department) : Result.error("未找到该二级学院");
    }

    // 添加二级学院
    @PostMapping("/add")
    public Result addDepartment(@RequestBody Department department) {
        boolean result = departmentService.addDepartment(department);
        return result ? Result.succeed("二级学院添加成功") : Result.error("二级学院添加失败");
    }

    // 更新二级学院信息
    @PutMapping("/update")
    public Result updateDepartment(@RequestBody Department department) {
        System.out.println(department.getDepID());
        boolean result = departmentService.updateDepartment(department);
        return result ? Result.succeed("二级学院信息更新成功") : Result.error("二级学院信息更新失败");
    }

    // 删除二级学院
    @DeleteMapping("/delete/{ID}")
    public Result deleteDepartment(@PathVariable String ID) {
        System.out.println("ID"+ID);
        boolean result = departmentService.deleteDepartmentByDepID(ID);
        return result ? Result.succeed("二级学院删除成功") : Result.error("二级学院删除失败");
    }
}
